package com.bmit.clusterspring.controller;

import com.bmit.clusterspring.common.StatusCode;
import com.bmit.clusterspring.model.BarVisualization;
import com.bmit.clusterspring.model.ScatterVisualization;
import com.bmit.clusterspring.service.KMeansService;
import com.bmit.clusterspring.util.IpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/kmeans")
public class KMeansController {
    @Autowired
    private KMeansService kMeansService;

    @RequestMapping("/upload")
    public StatusCode getCsv(HttpServletRequest request, @RequestParam MultipartFile file) {
        boolean isSuccess = false;
        if (!file.isEmpty()) {
            try {
                String ip = IpUtils.getIpAddr(request);
                isSuccess = kMeansService.uploadCSV(ip, file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return isSuccess ? StatusCode.Success : StatusCode.Fail;
    }

    @RequestMapping("/preview")
    public List<Map<String, String>> show(HttpServletRequest request) {
        String ip = IpUtils.getIpAddr(request);
        return kMeansService.showCSV(ip);
    }

    @RequestMapping("/strategy")
    public StatusCode setStrategy(HttpServletRequest request, @RequestParam String pre) {
        String ip = IpUtils.getIpAddr(request);
        kMeansService.setStrategy(ip, pre);
        return StatusCode.Success;
    }

    @RequestMapping("/params")
    public StatusCode setParams(HttpServletRequest request, @RequestParam String k) {
        String ip = IpUtils.getIpAddr(request);
        kMeansService.setParams(ip, k);
        return StatusCode.Success;
    }

    @RequestMapping("/columns")
    public StatusCode useColumns(HttpServletRequest request, @RequestParam List<String> cols) {
        String ip = IpUtils.getIpAddr(request);
        kMeansService.setColumns(ip, cols);
        return StatusCode.Success;
    }

    @RequestMapping("/cluster")
    public StatusCode cluster(HttpServletRequest request) {
        String ip = IpUtils.getIpAddr(request);
        kMeansService.runCluster(ip);
        return StatusCode.Success;
    }

    @RequestMapping("/result")
    public String downloadResult(HttpServletRequest request) {
        String ip = IpUtils.getIpAddr(request);
        return kMeansService.resultName(ip);
    }

    @RequestMapping("/sizeequals2")
    public boolean checkSizeEqual2(HttpServletRequest request) {
        String ip = IpUtils.getIpAddr(request);
        return kMeansService.checkSize(ip);
    }

    @RequestMapping("/scatter")
    public Map<String, ScatterVisualization> scatter(HttpServletRequest request) {
        String ip = IpUtils.getIpAddr(request);
        return kMeansService.scatter(ip);
    }

    @RequestMapping("/bar")
    public Map<String, BarVisualization> bar(HttpServletRequest request) {
        String ip = IpUtils.getIpAddr(request);
        return kMeansService.bar(ip);
    }

    @RequestMapping("/gettime")
    public String getTime(HttpServletRequest request){
        String ip = IpUtils.getIpAddr(request);
        return kMeansService.getTime(ip);
    }
}
